package com.hc.core.util;

import javax.servlet.http.HttpServletRequest;

import com.hc.core.Constants;

public class IpTool {
	public static String[] ip_array = StringUtil.split(Constants.LIMIT_IP,";");
	public static String split = "-";

	/**
	 * 检查访问者IP是否在ip段中
	 * 
	 * @param visitIp
	 * @return
	 */
	public static boolean checkVisitIp(String visitIp) {
		if (null == visitIp || visitIp.length() == 0) {
			return false;
		}

		if (checkIpArray(visitIp)) {//检查ip数组
			return true;
		} else {
			System.out.println("访问者IP:" + visitIp + "访问被禁止!");
			return false;
		}
	}

	/*
	 * 检查IP段
	 */
	public static boolean checkIpArray(String visit_ip) {
		boolean flag = false;
		String start_ip = null, end_ip = null;
		for (int i = 0; i < ip_array.length; i++) {
			start_ip = ip_array[i].substring(0, ip_array[i].indexOf(split));
			end_ip = ip_array[i].substring(ip_array[i].indexOf(split) + 1,
					ip_array[i].length());
			//System.out.println("start_ip = " + start_ip);
			//System.out.println("end_ip = " + end_ip);
			//System.out.println("visit_ip = " + visit_ip);
			if (ipToLong(visit_ip) >= ipToLong(start_ip)
					&& ipToLong(visit_ip) <= ipToLong(end_ip)) {
				flag = true;
				return flag;
			}
		}
		return flag;
	}

	/**
	 * 获得ip地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getVisitIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	// 将127.0.0.1 形式的IP地址转换成10进制整数
	public static long ipToLong(String strIP) {
		long[] ip = new long[4];
		int position1 = strIP.indexOf(".");
		int position2 = strIP.indexOf(".", position1 + 1);
		int position3 = strIP.indexOf(".", position2 + 1);
		ip[0] = Long.parseLong(strIP.substring(0, position1));
		ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2));
		ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3));
		ip[3] = Long.parseLong(strIP.substring(position3 + 1));
		return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
	}

	// 将10进制整数的IP地址转换成127.0.0.1 形式
	public String longToIP(long longIP) {
		StringBuffer sb = new StringBuffer("");
		sb.append(String.valueOf(longIP >>> 24));
		sb.append(".");
		sb.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16));
		sb.append(".");
		sb.append(String.valueOf((longIP & 0x0000FFFF) >>> 8));
		sb.append(".");
		sb.append(String.valueOf(longIP & 0x000000FF));
		return sb.toString();
	}

	public static void main(String[] arg) {
		System.out.println(IpTool.checkVisitIp("192.168.4.2"));
	}
}
